package com.ashlikun.frame.simple.wheel

import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.ashlikun.adapter.recyclerview.common.CommonAdapter
import com.ashlikun.frame.simple.R
import com.ashlikun.frame.simple.databinding.WheelviewItemBinding
import com.ashlikun.frame.simple.wheel.adapter.TestAdapter
import com.ashlikun.frame.simple.wheel.adapter.TestAdapter2
import com.ashlikun.frame.simple.wheel.adapter.UserBean
import com.ashlikun.utils.ui.extend.click
import com.ashlikun.wheelview_rv.WheelRecyclerView

class WellMainActivity : AppCompatActivity() {
    private var context: Context? = null
    private var wv1: WheelRecyclerView? = null
    private var wv2: WheelRecyclerView? = null
    private var wv3: WheelRecyclerView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main_wheel)
        initView()
        context = this
    }

    val adapter2 by lazy {
        CommonAdapter<UserBean>(this, binding = WheelviewItemBinding::class.java, initDatas = TestDatas.createTestDatas(),
            convert = {
                binding<WheelviewItemBinding> {
                    ivHead.setImageResource(it.drawable)
                    tvName.text = it.name + positionBind
                    if (wv2!!.currentItem == positionBind) {
                        tvName.setTextColor(Color.RED)
                    } else {
                        tvName.setTextColor(Color.BLACK)
                    }
                }
            })
    }

    /**
     * 如果需要刷新时可以使用WheelAdapter.notifyDataSetChanged()
     */
    private fun initView() {
        findViewById<View>(R.id.btn1).click {
            wv2!!.setCurrentItem(18)
        }

        wv1 = findViewById<View>(R.id.wv1) as WheelRecyclerView
        val userBeanList1 = TestDatas.createTestDatas()
        val w1Ada = TestAdapter(userBeanList1)
        wv1!!.adapter = w1Ada
        wv1!!.onSelected = {
            Toast.makeText(context, userBeanList1[it].name, Toast.LENGTH_SHORT).show()
            w1Ada.currentSelect = it
            w1Ada.notifyDataSetChanged()
        }
        wv1!!.onScrolled = { index ->
            Log.e("onScroll", "onScroll position ${index}")
        }
        wv2 = findViewById<View>(R.id.wv2) as WheelRecyclerView
        wv2!!.adapter = adapter2
        wv2!!.onSelected = {
            adapter2.notifyDataSetChanged()
            Toast.makeText(context, adapter2.getItemData(it)?.name, Toast.LENGTH_SHORT).show()
        }
        wv2!!.onScrolled = { index ->
            Log.e("onScroll", "onScroll2 position ${index}")
        }
        wv3 = findViewById<View>(R.id.wv3) as WheelRecyclerView
        val userBeanList3 = TestDatas.createTestDatas()
        wv3!!.adapter = TestAdapter2(userBeanList3)
        wv3!!.onSelected = {
            Toast.makeText(context, userBeanList3[it].name, Toast.LENGTH_SHORT).show()
        }
    }
}
